﻿/* 
 * BtCommLib - the OpenWebNet .NET Library
 *
 * Copyright (C) 2011 BTicino S.p.A.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BTicino.BtCommLib.Domain.LowLevel;
using BTicino.BtCommLib.Domain.Util;

namespace BTicino.BtCommLib.Communication
{
    /// <summary>
    /// IBTCommChan is the interface that provides all the generic communication service
    /// that every application will use to connect Bticino gateway
    /// 
    /// Since IBTCommChan is a sort of abstract class the application code obtains a concrete communication channel
    /// instace from BTCommMgr that is a singleton class responsible to provide specific communication channel instances
    /// according to the required channel type
    /// </summary>
    public abstract class  IBTCommChan
    {

        public delegate void NotifyEvent(object highLevelEventMsg);

        public delegate void NotifyError(BTCommErr highLevelErrorMsg);

        public event NotifyEvent notifyEvent;

        public event NotifyError notifyError;

        /// <summary>
        /// Stars connection procedure towards specific Bticino device
        /// </summary>
        /// <param name="theChanPar">Communication channel parameter dictionary</param>
        /// <returns>Return BTCommErr enumearation</returns>
        public abstract BTCommErr connect(BTCommChanPar theChanPar);


        /// <summary>
        /// Disconnect from BTicino gateway taking care of all the underling links (e.g. command and monitor link)
        /// </summary>
        public abstract void disconnect();


        /// <summary>
        /// Check the connectionstatus
        /// </summary>
        /// <returns>True if all the underling links (e.g. command and monitor link) are connected, False if they are not connected</returns>
        public abstract bool isConnected();


        /// <summary>
        /// Send Message, written in high level format, to BTicino devices and gives back replay frames.
        /// </summary>
        /// <param name="theMsg">high Msg to send</param>
        /// <param name="theResult">Frame received form BTicino devices</param>
        /// <returns>BTCommErr object</returns>
        public abstract BTCommErr Send(object theMsg);


        /// <summary>
        /// Send Message, written in low level string format, to BTicino devices and gives back replay frames.
        /// </summary>
        /// <param name="theMsg">low string Msg to send</param>
        /// <param name="theResult">Frame received form BTicino devices</param>
        /// <returns>BTCommErr object</returns>
        public abstract BTCommErr Send(string theMsg, out List<string> theResult);


        /// <summary>
        /// Send Message, write in high level languge, to BTicino devices and gives back replay frames.
        /// </summary>
        /// <param name="theMsg">high Msg to send</param>
        /// <param name="theResult">Frame received form BTicino devices</param>
        /// <returns>BTCommErr object</returns>
        public abstract BTCommErr Send(object theMsg, out List<object> theResult);


        /// <summary>
        /// Call Event Handle
        /// </summary>
        /// <param name="type">Even status name</param>
        /// <param name="targetEventMsg">Event status class type</param>
        public abstract void HandleEvent(string type, Type targetEventMsg);


        /// <summary>
        /// Remove specific Handle instance
        /// </summary>
        /// <param name="keyType">Handle name</param>
        public abstract void RemoveHandleEvent(string type);


        /// <summary>
        /// Remove all Handle istances
        /// </summary>
        public abstract void RemoveAllHandleEvents();


        /// <summary>
        /// Invoke made by a Low Level Call
        /// </summary>
        /// <param name="lowLevelFrame"></param>
        protected abstract void invokeEventHandler(string lowLevelFrame);


 
        protected void riseError(BTCommErr highLevelErrorMsg)    {
            notifyError(highLevelErrorMsg);
        }


        protected void riseEvent(object highLevelEventMsg)
        {
            notifyEvent(highLevelEventMsg);
        }
    }
}
